<template>
  <el-form ref="addForm" :model="formData" :rules="rules" label-width="80px">
    <el-form-item label="姓名" prop="username">
      <el-input v-model="formData.username" placeholder="请输入姓名" />
    </el-form-item>
    <el-form-item label="手机" prop="mobile">
      <el-input v-model="formData.mobile" placeholder="请输入姓名" />
    </el-form-item>
    <el-form-item label="入职时间" prop="timeOfEntry">
      <el-date-picker
        v-model="formData.timeOfEntry"
        type="date"
        placeholder="请选择入职时间"
        style="width: 100%"
      />
    </el-form-item>
    <el-form-item label="聘用形式" prop="formOfEmployment">
      <el-select
        v-model="formData.formOfEmployment"
        style="width: 100%"
        placeholder="请选择"
      >
        <el-option
          v-for="item in hireType"
          :key="item.id"
          :label="item.value"
          :value="item.id"
        />
      </el-select>
    </el-form-item>

    <el-form-item label="工号" prop="workNumber">
      <el-input v-model="formData.workNumber" placeholder="请输入姓名" />
    </el-form-item>
    <el-form-item label="部门" prop="departmentName">
      <div class="trrInput">
        <el-input
          v-model="formData.departmentName"
          placeholder="请输选择部门"
          class="boxinput"
          @focus="hFocus"
        />
        <el-collapse-transition>
          <div v-if="showTree">
            <el-tree
              :data="treeData"
              :props="{ label: 'name' }"
              @node-click="NodeClick"
            />
          </div>
        </el-collapse-transition>
      </div>
    </el-form-item>
    <el-form-item label="转正时间" prop="correctionTime">
      <el-date-picker
        v-model="formData.correctionTime"
        type="date"
        placeholder="请选择转正时间"
        style="width: 100%"
      />
    </el-form-item>
    <span class="dialog-footer">
      <el-button @click="hCancel">取 消</el-button>
      <el-button type="primary" @click="hSubmit">确 定</el-button>
    </span>
  </el-form>
</template>
<script>
import EmployeeEnum from '@/constant/employees.js'
import { getDepartments } from '@/api/departments.js'
import { addEmployee } from '@/api/employees.js'
import { ToDoLostData } from '@/utils'
export default {
  name: '',
  data() {
    return {
      // 聘用形式
      hireType: EmployeeEnum.hireType,
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      //  员工列表
      treeData: [],
      showTree: false,
      // 表单校验
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式不正确',
            trigger: 'blur'
          }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  created() {
    // this.loadDepartments()
  },
  methods: {
    hCancel() {
      this.$emit('close')
    },
    // 获取部门信息
    async loadDepartments() {
      try {
        const { data } = await getDepartments()
        // console.log(data.depts)
        data.depts.shift() // 去掉第一个元素（公司名）
        this.treeData = ToDoLostData(data.depts, '')
        // 显示列表
        this.showTree = true
      } catch (error) {
        console.log(error)
      }
    },
    // 添加部门
    async doAdd() {
      try {
        await addEmployee(this.formData)
        this.$message.success('添加成功')
        // 通知父组件
        this.$emit('update-empolyee')
        this.$emit('close')
      } catch (error) {
        this.$message.error('添加失败')
        console.log(error)
      }
    },
    hSubmit() {
      // 兜底校验
      this.$refs.addForm.validate(vali => {
        if (vali) {
          // 做添加
          this.doAdd()
        }
      })
    },
    // 鼠标获取焦点时候触发
    hFocus() {
      this.loadDepartments()
    },
    // 用户在部门结构树上点击
    NodeClick(data, node) {
      // 如果当前部门还有子部门，则不能选中
      if (data.children) return
      // 将当前选中的显示在input 中
      this.formData.departmentName = data.name
      // 2. 隐藏 tree
      this.showTree = false
    },
    // 重置表单
    resetForm() {
      this.formData = {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      }

      this.$refs.addForm.resetFields()
    }
  }
}
</script>
<style lang="scss">
.department-container {
  .el-dialog__body {
    padding: 0 !important;
  }
}
.boxinput .el-input__inner {
  border: 0;
}
.trrInput {
  position: absolute;
  width: 100%;
  z-index: 9999;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
}
.trrInput:hover {
  border: 1px solid #bcbec3;
  // -webkit-box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
  // box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
}
</style>
<style lang="scss" scoped>
.dialog-footer {
  display: inline-block;
  width: 100%;
  text-align: right;
}
.el-form {
  padding: 30px 20px;
}
</style>
